<?php
namespace common\models;

use Yii;

/**
 * Oauth2.0 用户授权依赖模型
 * @author CGA
 */
class ARUser extends \common\models\User implements \OAuth2\Storage\UserCredentialsInterface
{

    const ROLE_USER = 1; //普通用户
    const ROLE_AGENT = 2; //经纪人
    const ROLE_PLATFORM=3;   //平台
    const ROLE_ADMIN = 9; //管理员

    const STATUS_ACTIVE = 99; //正常使用
    const STATUS_INACTIVE = -1; //停用

    const REAL_AUTH = 99; //已认证
    const REAL_UNAUTH = -1; //未认证

    public function attributeLabels()
    {
        return [
            'role'                 => '角色',
            'username'             => '用户名',
            'auth_key'             => '自动登录key',
            'password_hash'        => '登陆密码哈希',
            'payword_hash'         => '交易密码哈希',
            'password_reset_token' => '重置登录密码token',
            'email'                => '绑定邮箱',
            'country'              => '手机所属国家',
            'mobile'               => '绑定手机号',
            'is_real'              => '是否实名',
            'status'               => '用户状态',
            'lastlogin_at'         => '最后登陆时间',
            'created_at'           => '注册时间',
            'updated_at'           => '更新时间',
        ];
    }

    /**
     * Implemented for Oauth2 Interface
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        $module = Yii::$app->getModule('oauth2');
        $token = $module->getServer()->getResourceController()->getToken();
        return !empty($token['user_id'])
        ? static::findIdentity($token['user_id'])
        : null;
    }

    /**
     * Implemented for Oauth2 Interface
     */
    public function checkUserCredentials($username, $password)
    {
        $user = static::findByUsername($username);
        if (empty($user)) {
            return false;
        }
        return $user->validatePassword($password);
    }

    /**
     * Implemented for Oauth2 Interface  --  email
     */
    public function emailCheckUserCredentials($email, $password)
    {
        $user = static::findByEmail($email);
        if (empty($user)) {
            return false;
        }
        return $user->validatePassword($password);
    }

    /**
     * Implemented for Oauth2 Interface  --  mobile
     */
    public function mobileCheckUserCredentials($mobile, $password)
    {
        $user = static::findByMobile($mobile);
        if (empty($user)) {
            return false;
        }
        return $user->validatePassword($password);
    }

    /**
     * Implemented for Oauth2 Interface
     */
    public function getUserDetails($username)
    {
        $user = static::findByUsername($username);
        return ['user_id' => $user->getId()];
    }

    /**
     * Implemented for Oauth2 Interface  --  email
     */
    public function getUserDetailsByEmail($email)
    {
        $user = static::findByEmail($email);
        return ['user_id' => $user->getId()];
    }

    /**
     * Implemented for Oauth2 Interface  --  mobile
     */
    public function getUserDetailsByMobile($mobile)
    {
        $user = static::findByMobile($mobile);
        return ['user_id' => $user->getId()];
    }
    /**
     * Finds user by username
     *
     * @param  string        $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE, 'role' => [self::ROLE_PLATFORM, self::ROLE_ADMIN]]);
    }

    /**
     * Finds user by email
     *
     * @param  string        $email
     * @return static|null
     */
    public static function findByEmail($email)
    {
        return static::findOne(['email' => $email, 'status' => self::STATUS_ACTIVE]);
    }

    /**
     * Finds user by mobile
     *
     * @param  string        $mobile
     * @return static|null
     */
    public static function findByMobile($mobile)
    {
        return static::findOne(['mobile' => $mobile, 'status' => self::STATUS_ACTIVE]);
    }

    /**
     * Validates password
     *
     * @param  string $password password to validate
     * @return bool   if password provided is valid for current user
     */
    public function validatePayword($payword)
    {
        return Yii::$app->security->validatePassword($payword, $this->payword_hash);
    }
    
    /**
     * 获取评分信息
     */
    public function getScoring()
    {
        return $this->hasOne(ARUserScoring::className(), ['uid' => 'id']);
    }
    
    /**
     * 获取经纪人评分信息
     */
    public function getBrokerScoring()
    {
        return $this->hasOne(ARBrokerScoring::className(), ['uid' => 'id']);
    }
}
